package mq.job;

import common.MyDisReentrantLock;
import lombok.extern.slf4j.Slf4j;
import mq.service.IMqMailService;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.springframework.scheduling.quartz.QuartzJobBean;

import javax.annotation.Resource;

/**
 * @description: MQ异常记录邮件预警
 * @author: zk
 * @since: 2022-08-21 09:26:41
 */
@Slf4j
public class MqMailJob extends QuartzJobBean {
    private final String MQ_MAIL_LOCK = "MQ_MAIL_LOCK";
    @Resource
    private MyDisReentrantLock myDisReentrantLock;
    @Resource
    private IMqMailService mqMailService;


    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("MQ异常记录邮件预警定时任务 begin");
        boolean lock = myDisReentrantLock.lock(MqMailJob.class, MQ_MAIL_LOCK, 7200);
        if (!lock) {
            log.info("MQ异常记录邮件预警定时任务,已经有任务在处理 end");
            return;
        }
        try {
            mqMailService.sendMail();
        } catch (Exception e) {
            log.error("MQ异常记录邮件预警执行失败", e);
        } finally {
            myDisReentrantLock.unLock(MqMailJob.class, MQ_MAIL_LOCK);
            log.info("MQ异常记录邮件预警定时任务 end");
        }
    }
}
